﻿##
##    Document   : allstart.py
##    Author     : Ángel Fernández Pineda
##    Copyright  : 2011, Ángel Fernández Pineda, Spain
##    License    : Gnu General Public License - see LICENSE.TXT
##    Description: Main program script for any XBMC plugin
##
##    This file is free software: you can redistribute
##    it and/or modify it under the terms of the GNU General Public License as
##    published by the Free Software Foundation, either version 3 of the License,
##    or (at your option) any later version.
##
##    This file is distributed in the hope that it will
##    be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with the "Jamendo Radio plugin for XBMC".
##    If not, see <http://www.gnu.org/licenses/>.
##
"""
    Generic XBMC plugin script

    This code is valid for all XBMC addons of type 'plugin' without
    changes. Actual plugin code must be located at resources/lib/plugin.py
    and implemented as the function 'start(PluginInstance)'.

    addon.xml should reference this module in this way:

        <extension point="xbmc.python.pluginsource" library="allstart.py">

    Argument to plugin.start() is a static class exposing plugin parameters
    and helper methods:

    * PluginInstance.id

        Instance identifier, required by xbmcplugin module

    * PluginInstance.name

        Current plugin name, which is the same as its folder's name
        under XBMC/addons. For example: 'plugin.video.myplugin'

    * PluginInstance.path and PluginInstance.resourcesPath

        Plugin path and resources path. Already translated.

    * PluginInstance.data

        Data associated with the directory item that the user has selected,
        which was previously generated by xbmcplugin.addDirectoryItem()
        and encoded with PluginInstance.url()

    * PluginInstance.url()

        Encode any kind of data as an URL which should be passed to
        xbmcplugin.addDirectoryItem()

    * PluginInstance.setting()

        Retrives a user-configured setting as defined in settings.xml

    * PluginInstance.string()

        Retrives a localized string as defined in strings.xml

    Do not overwrite those properties and methods.
"""

# Define plugin initialization class.
# Methods and attributes are static.

class PluginInstance:
    """
        Parameters and settings for a XBMC plugin
    """

    # Set plugin name and ID
    import sys
    name = sys.argv[0].replace('plugin://','')[:-1]
    id = int(sys.argv[1])

    # Add resources/lib to library path and set some attributes
    import xbmcaddon
    import xbmc
    import os
    addon = xbmcaddon.Addon(name)
    path = xbmc.translatePath(addon.getAddonInfo('path'))
    resourcesPath = os.path.join(path,'resources') 
    sys.path.append(os.path.join(resourcesPath,'lib'))

    # Set parameter data
    import urllib
    if (len(sys.argv)<3) or (len(sys.argv[2])==0):
        data = None
    else:
        data = eval(urllib.unquote(sys.argv[2].replace("?data=","")))
    #end if

    # Helper methods
    
    @staticmethod
    def url(anydata):
        """
            Generates a plugin:// URL in order to pass data to
            next plugin instance

            @param anydata Any type of data
            @return string plugin://plugin_name/?data=str
                where str is a string representation of anydata
        """
        return (PluginInstance.sys.argv[0]+'?data='+
            PluginInstance.urllib.quote(repr(anydata)))
    #end def

    #import xbmcplugin
    @staticmethod
    def setting(setting_name):
        """
            Retrieves a setting value as defined in settings.xml

            @param setting_name string The name of a setting
            @return variant User value for setting_name
        """
        return PluginInstance.addon.getSetting(setting_name)
    #end def
    
    @staticmethod
    def string(string_id):
        """
            Retrives a localized string as defined in strings.xml

            @param string_id integer Identifier of the required string
            @return string The required localized string
        """
        if (string_id<30000):
            return xbmc.getLocalizedString(string_id)
        else:
            return (PluginInstance.addon.getLocalizedString(string_id))
    #end def
#end class

# Invoke plugin implementation

import plugin
plugin.start(PluginInstance)
